SYNTAX OF COMMANDS


Overview

The syntax of the commands that SRename understands is relatively straightforward. After giving the filename(s) or file pattern(s) to rename you simply supply an Action that will modify those filenames or patterns.
If you don't want to modify the entire filename but only a part of it you must also supply one or more Selectors before that action.
If you want to modify the filename in a somewhat complex way you will have to give more than one action. In that case it makes sense that each action should modify a different part of the filename otherwise each action would overwrite the previous one. So to do that selectors have to be given before each action.

So basically the command syntax looks like this (brackets indicate optional arguments) :

FILENAME(S) or FILEPATTERN(S) [SELECTOR(S)] ACTION [ [SELECTOR(S)] ACTION [SELECTOR(S)] ACTION .....]

After each action is executed the selectors are cleared so they have to be given again for the next action, unless Reuse which is described below is active. If no selectors are given before an action then the whole filename is modified.

For example: SRename ram:anyfile LOWER LEFT 1 UPPER RIGHT 1 UPPER
Here "LOWER" makes the whole filename lowercase because no selector is given before it, and then the first and last characters are made uppercase with "LEFT 1 UPPER" and "RIGHT 1 UPPER".

There are a number of General Options that SRename also accepts and these can be placed anywhere in the command line but for the sake of clarity it's better to put them at the beginning. These general options affect every selector and action that will be performed and should only be given once in the command line.
There are also some options that affect selectors and some options that affect actions. Selector Options should be placed before the selectors of each action, with the exception of INVERTSEL that should be placed after the selectors, and Action Options should be placed after each action.
Finally some selectors and actions accept Secondary Arguments that should be placed immediately after those selector and actions.

Taking all those details into account the command syntax looks like:

FILENAME(S) or FILEPATERN(S) [GENERAL OPTIONS] [SELECTOR OPTIONS] [SELECTOR & SECONDARY ARGUMENTS] [SELECTOR & SECONDARY ARGUMENTS] ACTION [ACTION OPTIONS] ... more selectors and actions...
 
Example of such a command line:
SRename sys:a#? ram:#?s ALL FILES PREFIRST PREFIX MATCH mod CASESENS LEFT 1 UPPER TOCOMMENT ... more selectors and actions...

In this example "ALL" and "FILES" are general options, "PREFIRST" is a selector option, "PREFIX", "MATCH mod" and "LEFT 1" are selectors, "CASESENS" is a secondary argument to "MATCH mod", "UPPER" is an action, and "TOCOMMENT" is an action option.


Reuse

Up to now SRename could only work in an "immediate" mode. That is, every given action would immediately modify the filename and the next action would act on that modified filename. This doesn't matter much for simple renaming operations but it raises obstacles for more complex operations.
For example if you wanted to swap one character with another this wasn't possible up to now because SRename worked as described above.

To perform a character swap of "+" with "-" you would give: MATCH + TO - MATCH - TO +

but this wouldn't work because the first action (MATCH + TO -) would change every plus sign to a minus sign and the second action would act on that result, so in the end we would end up only with plus signs.

From SRename 3.5.0 using Reuse the previous operation can be done by simply enclosing the two actions in parentheses. Note that there must be a space between and after each parenthesis:
SRename <filename> ( MATCH + TO - MATCH - TO + )

Enclosing actions in parentheses means that the enclosed actions will act on the resulting filename before the first parenthesis - will reuse that filename - , so in the previous example both of the given actions will act on the original unmodified filename.

If there are other actions before the actions in parentheses then the actions in parentheses will act on the result of the actions before them.
Example :
SRename <filename> MATCH " " DELETE UP ( MATCH + TO - MATCH - TO + )

In the above example the spaces will be removed, the filename will be made uppercase and then the 2 actions in parentheses will act on the resulting filename of the first 2 actions.

Any actions (in parentheses or not) after actions in parentheses will act on the result of those actions in parentheses. For example:
SRename <filename> (MATCH + TO - MATCH - TO + ) MATCH % DELETE

Here the last action ( MATCH % DELETE) will act on the result from the 2 actions in parentheses.

Reuse can also be used to avoid giving the same selectors multiple times. Any selectors or selector options before the actions in parentheses are reused for those actions. For example :

( SUFFIX WORD 1 MATCH abcd LEFT 1 UP SUFFIX WORD 1 MATCH abcd RIGHT 1 UP ) can be replaced with the shorter :
SUFFIX WORD 1 MATCH abcd ( LEFT 1 UP RIGHT 1 UP )

Note that it doesn't make sense putting less than 2 actions in parentheses.
Also note that component actions can't utilize Reuse so they shouldn't be placed inside parentheses. If they are this will result in that the action after a component action will act not on the resulting filename before the actions in parentheses, but on the result of that component action.
Finally the user should keep in mind that Reuse can have some interesting side effects when the same group of characters is modified by multiple actions.
For example if the 5 rightmost characters of a filename are deleted and then the 2 rightmost characters are made uppercase, because both actions are modifying the original filename the result will be that the 2 rightmost characters will be uppercase and the other 3 characters next to them will be deleted.


More details

Selectors and Actions can be given multiple times in the command line, but Renumbering Selectors and Actions can only be given once, because currently SRename doesn't support multiple renumbering actions of the same type at the same time in the command line.
Because of this the order that Renumbering Options, Selectors and Actions are given doesn't matter, but for the sake of consistency it's better to give Renumbering commands in the same way that the other commands are given:
Renumbering Options first, then Renumbering Selectors and Renumbering Actions last.

The order that selectors are given is not important right now because the order that they are evaluated is fixed.
That may change in the future however and in order to maintain command interface compatibility in future versions, it is recommended that you give selectors in the command line in the order that they are evaluated.
That order is:

COMMENT (CO)
PREFIX (PR)
MAIN (MA)
SUFFIX (SU)
WORD (WD)
MATCH (MT)
LEFT (L) *
MID (M) *
RIGHT (R) *

*
Can use only one of those selectors for each action.

This fixed order of evaluation mean that you can select for example the suffix, then a word in suffix, then match a string in the word and then select the leftmost character in that string, but you can't for example select the 10 leftmost characters and then match a string in those, because MATCH will be evaluated before LEFT.

And of course you can't use the same selector more than once for each action. For example you can't select the 10 leftmost characters and then the 3 leftmost characters of those 10 characters.

Because the PREFIX, MAIN and SUFFIX selectors can be used all at once with the same action, every selector that will be given after these 3 selectors and the action that uses them, will be repeated for each prefix, main and suffix components of the filename.
For example if "MAIN SUFFIX MATCH document LEFT 1 UP" is used then SRename will try to match the string "document" and select it's first character, for both the main and the suffix components.

The order that actions inside parentheses are given matters as far as the results of one action overwrite the results of the other. In such a case the results of actions at the beginning can obviously be overwritten by the results of actions after them.

The order that secondary arguments are given after a selector or action doesn't matter as soon as those secondary arguments are given immediately after the relevant selector or action. For example:

"MATCH abcd DO 1 SKIP 1 REVMATCH" and "MATCH abcd SKIP 1 REVMATCH DO 1" do the same thing.